<template>
  <div class="empower">
    <div class="empower-content">
      <img src="../../assets/images/login_left.png" alt="" srcset="" style="height: 100%;" class="login-left">
      <div class="empower-list">
        <p class="empower-title">使用 统一认证平台 授权并登录</p>
        <ul class="list-content">
          <li><span class="list-dots"></span>允许访问您的用户详细信息</li>

          <div class="empower-botton">
            <el-button type="primary" @click="authorize" :loading="buttonLoading">同意并继续</el-button>
          </div>
        </ul>
        
      </div>
    </div>
  </div>
</template>
<script>
export default { name: 'login' }
</script>
<script setup>
import { ref, reactive, onMounted, watch } from 'vue';
import { User, Lock } from '@element-plus/icons-vue'
import { useRouter } from 'vue-router'
import { ElMessage, ElMessageBox } from 'element-plus'
import { http } from '@/utils/http/index';
import { useAuthStore } from '@/stores/modules/auth.js'
import initRequest from '@/utils/http/request'

/* 路由 */
const router = useRouter()

const authStore = useAuthStore()

const buttonLoading = ref(false)

const authorize = () => {
  buttonLoading.value = true
  let params = {
    AppId: router.currentRoute.value.query.AppId ? router.currentRoute.value.query.AppId : '' ,
    Scope: router.currentRoute.value.query.Scope ? router.currentRoute.value.query.Scope : router.currentRoute.value.query.AppId ? 'auth_base' : '',
    RedirectUri: router.currentRoute.value.query.RedirectUri ? router.currentRoute.value.query.RedirectUri : '',
    Id: authStore.base_auth_userinfo.id ? authStore.base_auth_userinfo.id : '',
    AppAccessToken: authStore.base_auth_token ? authStore.base_auth_token : '',
  }

  const formData = new FormData()
    for(let k in params) {
      formData.append(k ,params[k])
  }

  http.$POST("/OAuth2/AuthorizeDo", formData).then(res => {
    buttonLoading.value = false
    
    if(res.isError){
      ElMessage.warning(res.message);
      return router.push({
        path: '/Home/Login',
        query: router.currentRoute.value.query
      })
    }

    /* 处理跳转 */
    if(res.result) {
      window.open(res.result, "_self")
    }
  }).catch(() => {
    buttonLoading.value = false
    ElMessage.warning('授权失败')
  })
}

onMounted(() => {
  authorize()
})
</script>

<style lang='scss' scoped>
.curP {
  cursor: pointer;
}
.empower {
  width: 100%;
  height: 100vh;
  overflow: hidden;
  background: url("../../assets/images/login_bg.png");
  background-size: 100%;
  display: flex;
  justify-content: center;
  align-items: center;

  .empower-content {
    max-width: 1128px;
    background: #ffffff;
    height: 720px;
    border-radius: 31px;
    display: flex;
    position: relative;
  }

  .empower-list {
    width: 668px;
    height: 100%;
    background: #fff;
    border-radius: 15px;
    padding: 50px 90px 104px 104px;
  }

  .empower-title{
    color: #111111;
    font-weight: bold;
    font-size: 24px;
  }

  .list-content{
    list-style: none;
    line-height: 30px;
    margin-top: 30px;
    position: relative;
    height: 90%;
    .list-dots{
      display: inline-block;
      width: 8px;
      height: 8px;
      border-radius: 50%;
      background: #999999;
      margin-right: 8px;
      line-height: 30px;
    }
  }

  .empower-botton{
    position: absolute;
    bottom: 0;
    width: 100%;
    text-align: center;
    :deep(.el-button--primary) {
      width: 100%;
      height: 48px;
      line-height: 48px;
      color: #FFFFFF;
      font-size: 22px;
      background: #7377D2;
      border-color: #7377D2;
    }
  }

  .empower-logout{
    color: #999999;
    margin-top: 10px;
  }
}

@media screen and (max-width : 1000px) {
  .empower-list {
    width: 100% !important;
    position: absolute;
    top: 0;
    left: 0;
    border-radius: 15px !important;
  }
  .login-left{
    border-radius: 15px !important;
  }
}
</style>